package sk.fritz.megafonsms.formmodel;

import java.util.Enumeration;
import java.util.Vector;

public class FormModel {
	
	private String formId;
	
	private String formAction;
	
	private String formMethod;
	
	private String encType;
	
	private Vector formControls = new Vector(10);
	
	public AbstractFormControl getControl(String name) {
		Enumeration e = getFormControls();
		while (e.hasMoreElements()) {
			AbstractFormControl f = (AbstractFormControl) e.nextElement();
			if (f.getName().equals(name))
				return f;
		}
		return null;
	}
	
	public void addFormControl(AbstractFormControl control) {
		formControls.addElement(control);
	}
	
	public Enumeration getFormControls() {
		return formControls.elements();
	}
	
	public Enumeration getSuccessfulControls() {
		Vector successful = new Vector();
		Enumeration en = getFormControls();
		while (en.hasMoreElements()) {
			AbstractFormControl ctrl = (AbstractFormControl) en.nextElement();
			if (ctrl instanceof InputFormControl) {
				InputFormControl ifc = (InputFormControl) ctrl;
				// all "on" checkboxes are successful
				if (ifc.getType() == InputFormControl.TYPE_CHECKBOX) {
					if (ifc.getValue() != null && ifc.getValue().equals("on"))
						successful.addElement(ctrl);
					continue;
				}
			}
			// controls without value set are sucessful
			if (ctrl.getValue() != null)
				successful.addElement(ctrl);
		}
		return successful.elements();
	}
	

	public String getFormId() {
		return formId;
	}

	public void setFormId(String formId) {
		this.formId = formId;
	}

	public String getFormAction() {
		return formAction;
	}

	public void setFormAction(String formAction) {
		this.formAction = formAction;
	}

	public String getFormMethod() {
		return formMethod;
	}

	public void setFormMethod(String formMethod) {
		this.formMethod = formMethod;
	}

	public String getEncType() {
		return encType;
	}

	public void setEncType(String encType) {
		this.encType = encType;
	}

}
